package thread.java20230113;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 多个线程, 同时往hashmap放值
 */
public class Demo55 {
//    static HashMap<Integer,Integer> hashMap = new HashMap<>(1024);
    static ConcurrentHashMap<Integer,Integer> hashMap = new ConcurrentHashMap<>(1024);
    public static void main(String[] args) throws InterruptedException {
        int size = 1000;//任务个数
        CountDownLatch latch = new CountDownLatch(size);
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 0; i < size; i++) {
            int finalI = i;
            executorService.execute(()->{
                hashMap.put(finalI,finalI);
                latch.countDown();
            });
        }
        latch.await();
        executorService.shutdown();//销毁线程池
        System.out.println(hashMap.size());
    }

}
